<?php
/**
 * 许多控制器的基类   主要用于判断是否登录  还有权限的检测
 */
require_once __ROOT__."/tayphp/Controller.class.php";
class baseController extends Controller{
	/**
	 * 登录检测   如果没有登录则跳转到登录页面
	 */
	public function __construct() {
		parent::__construct();
		session_start();
		if (!isset($_SESSION["id"])) {
			if(IS_PC){
				$this->redirect("Account/login");
			} else{
				$this->ajaxReturn($this->message(-2,"请登录!",""));
			}
			exit();
		} 
		session_write_close();
	}


	/**
	 * 查看教师是否有权限对某个课程进行操作
	 */
	public function coursePower(){
		session_start();
		if($_SESSION["isTeacher"] == "true"){
			$where["teacher_tea_id"] = $_SESSION["id"];
		} else{
			return $this->message(0,"你没有权限对该课程进行操作!","");
		}
		session_write_close();
		if(isset($_GET["cd"]) && !empty($_GET["cd"])){
			$where["cou_id"] = $_GET["cd"];
		} else if(isset($_POST["cou_id"]) && !empty($_POST["cou_id"])) {
			$where["cou_id"] = $_POST["cou_id"];
		} else{
			return $this->message(0,"你没有权限对该课程进行操作!","");
		}
		if(D()->table(array("course"=>""))->where($where)->count() > 0){
			return $this->message(1,"你拥有权限对该课程进行操作!","");
		} else{
			return $this->message(0,"你没有权限对该课程进行操作!","");
		}
	}

	/**
	 * 查看教师是否有权限对某个班级进行操作
	 */
	public function classPower(){
		if(isset($_GET["cad"]) && !empty($_GET["cad"])) {
			$where["cla.cla_id"] = $_GET["cad"];
		} else if(isset($_POST["cla_id"]) && !empty($_POST["cla_id"])){
			$where["cla.cla_id"] = $_POST["cla_id"];
		} else{
			return $this->message(0,"您没有权限对该班级进行操作!","");
		}
		session_start();
		if($_SESSION["isTeacher"] == "true"){
			$where["cou.teacher_tea_id"] = $_SESSION["id"];
		} else{
			return $this->message(0,"你没有权限对该班级进行操作!","");
		}
		session_write_close();
		$where["__string__"] = "cou.cou_id = cla.cou_id";
		if(D()->table(array("course"=>"cou","class"=>"cla"))->where($where)->count() > 0){
			$data = $this->message(1,"您可以对该班级进行操作!","");
		} else{
			$data = $this->message(0,"您没有权限对该班级进行操作!","");
		}
		unset($where);
		return $data;
	}

	/**
	 * 查看教师是否有权限对某个试卷进行操作!
	 */
	public function quizPower(){
		session_start();
		if($_SESSION["isTeacher"] == "true"){
			$where["cou.teacher_tea_id"] = $_SESSION["id"];
		} else{
			return $this->message(0,"你没有权限对该课程进行操作!","");
		}
		session_write_close();
		$where["qus.qus_id"] = $_POST["qus_id"];
		$where["__string__"] = "qus.class_cla_id = cla.cla_id and cla.cou_id = cou.cou_id";
		if(D()->table(array("question_set"=>"qus","class"=>"cla","course"=>"cou"))->where($where)->count() > 0){
			$data = $this->message(1,"您可以对该试卷进行操作!","");
		} else{
			$data = $this->message(0,"您没有权限对该试卷进行操作!","");
		}
		unset($where);
		return $data;
	}

	/**
	 * 检查教师是否有权限对某个题目进行操作!
	 */
	public function questionPower(){
		session_start();
		if($_SESSION["isTeacher"] == "true"){
			$where["cou.teacher_tea_id"] = $_SESSION["id"];
		} else{
			return $this->message(0,"你没有权限对该课程进行操作!","");
		}
		session_write_close();
		$where["que.que_id"] = $_POST["que_id"];
		$where["__string__"] = "que.qus_id = qus.qus_id and qus.class_cla_id = cla.cla_id and cla.cou_id = cou.cou_id";
		if(D()->table(array("question"=>"que","question_set"=>"qus","class"=>"cla","course"=>"cou"))->where($where)->count() > 0){
			$data = $this->message(1,"您可以对该题目进行操作!","");
		} else{
			$data = $this->message(0,"您没有权限对该题目进行操作!","");
		}
		unset($where);
		return $data;
	}

	/*
	检查学生是否有权限对班级进行操作的权限
	*/
	public function stuClassPower(){
		if(isset($_POST["cla_id"])){
			$where["class_cla_id"] = $_POST["cla_id"];
		} else if(isset($_GET["cad"])){
			$where["class_cla_id"] = $_GET["cad"];
		} else{
			return $this->message(0,"您没有权限对该班级进行操作!","");
		}
		session_start();
		if($_SESSION["isTeacher"] == "true"){
			return $this->message(0,"您没有权限对该班级进行操作!","");
		} else{
			$where["student_stu_id"] = $_SESSION["id"];
		}
		session_write_close();
		$where["is_pass"] = 1;
		if(D()->table(array("class_student"=>""))->where($where)->count() > 0){
			unset($where);
			return $this->message(1,"您可以对该班级进行操作!","");
		} else{
			unset($where);
			return $this->message(0,"您没有权限对该班级进行操作!","");
		}
	}
	/*
	检测学生是否有权限查看某个试卷得信息
	*/
	public function stuQuizPower(){
		if(isset($_POST["qus_id"])){
			$where["qs.qus_id"] = $_POST["qus_id"];
		} else if(isset($_GET["qs"])){
			$where["qs.qus_id"] = $_GET["qs"];
		} else{
			return $this->message(0,"您没有权限对该试卷进行操作!","");
		}
		session_start();
		if($_SESSION["isTeacher"] == "true"){
			return $this->message(0,"您没有权限对该试卷进行操作!","");
		} else{
			$where["cs.student_stu_id"] = $_SESSION["id"];
		}
		session_write_close();
		$where["cs.is_pass"] = 1;		//通过才有权限
		$where["__string__"] = "cs.class_cla_id = qs.class_cla_id";
		if(D()->table(array("class_student"=>"cs","question_set"=>"qs"))->where($where)->count() > 0){
			unset($where);
			return $this->message(1,"您可以对该试卷进行操作!","");
		} else{
			unset($where);
			return $this->message(0,"您没有权限对该试卷进行操作!","");
		}
	}

	/*
	检查学生是否有权对某个题目进行操作
	 */
	public function stuQuePower(){
		if(isset($_POST["que_id"])){
			$where["que.que_id"] = $_POST["que_id"];
		} else if(isset($_GET["qed"])){
			$where["que.que_id"] = $_GET["qed"];
		} else{
			return $this->message(0,"您没有权限对该试卷进行操作!","");
		}
		session_start();
		if($_SESSION["isTeacher"] == "true"){
			return $this->message(0,"您没有权限对该试卷进行操作!","");
		} else{
			$where["cs.student_stu_id"] = $_SESSION["id"];
		}
		session_write_close();
		$where["cs.is_pass"] = 1;		//通过才有权限
		$where["__string__"] = "que.qus_id = qs.qus_id and qs.class_cla_id = cs.class_cla_id";
		if(D()->table(array("question"=>"que","question_set"=>"qs","class_student"=>"cs"))->where($where)->count() > 0){
			unset($where);
			return $this->message(1,"您可以对该题目进行操作!","");
		} else{
			unset($where);
			return $this->message(0,"您没有权限对该题目进行操作!","");
		}
	}

  	/**
  	 * 析构函数不用写任何内容也是可以的
  	 */
  	public function __destruct(){}
  }